package com.syni.mdd.sjb.community.base.listener;

import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.community.base.entity.bo.UserData;
import com.syni.mdd.sjb.community.base.service.WxExternalContactService;
import com.syni.mdd.sjb.community.base.service.WxPermanentCodeService;
import com.syni.mdd.sjb.community.base.utils.CommonUtils;
import com.syni.mdd.sjb.community.base.utils.OAUtils;
import com.syni.mdd.sjb.community.base.utils.TokenUtils;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
@Log4j2
public class SyncExternalcontactListener implements ApplicationListener<SyncExternalcontactEvent> {

    @Autowired
    WxPermanentCodeService wxPermanentCodeService;

    @Autowired
    WxExternalContactService wxExternalContactService;

    @Override
    @Async
    public void onApplicationEvent(SyncExternalcontactEvent syncExternalcontactEvent) {
        String cropId = syncExternalcontactEvent.getCropId();
        log.info("userInfo后置逻辑");
        log.info("cropId:{}",cropId);
        //清空cropId

        //找到对应的授权码
        String permanentCode =  wxPermanentCodeService.getPermanentCodeByCorpId(cropId);
        String accessToken = OAUtils.getAccessTokenByPerCode(cropId,permanentCode);
        //获取所有全部部门
        Set<Integer> departments = CommonUtils.getDepartments(accessToken);
        //
        //wxExternalContactService.deleteContactList(cropId);
        //循环获取部分成员
        if(departments.size() > 0){
            Set<String> userIds = new HashSet<>();
            departments.forEach(ds ->{
                userIds.addAll(CommonUtils.getSimplelist(accessToken,ds));
            });
            //获取每个成员的客户列表
            Set<String> externalContacts = new HashSet<>();
            if(userIds.size() > 0){
                userIds.forEach(ui -> {
                    externalContacts.addAll(CommonUtils.getExternalcontactList(accessToken,ui));
                });
            }
            List<String> externalcontactData = new ArrayList<>();
            if(externalContacts.size() > 0){
                externalContacts.forEach(ec -> {
                    String result = CommonUtils.getExternalcontactDetail(accessToken,ec);
                    if(StringUtils.isNotBlank(result)) {
                        externalcontactData.add(result);
                    }
                });
            }
            if(externalcontactData.size() > 0){
                wxExternalContactService.addContactList(cropId,externalcontactData);
            }
        }
    }
}
